package com.example.controller;

import com.example.entity.*;
import com.example.service.OrdersService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;

/**
 * (Orders)表控制层
 *
 * @author makejava
 * @since 2022-06-06 23:17:19
 */
@Api("订单信息")
@Controller
@RequestMapping("/admin")
public class OrdersController {
    /**
     * 服务对象
     */
    @Resource
    private OrdersService ordersService;

    /**
     * 分页查询
     *
     * @param orders 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
//    @GetMapping
//    public ResponseEntity<Page<Orders>> queryByPage(Orders orders, PageRequest pageRequest) {
//        return ResponseEntity.ok(this.ordersService.queryByPage(orders, pageRequest));
//    }
    @GetMapping("/orders")
    public String queryAll(Orders orders,
                           @RequestParam(value = "pageNum",required = false,defaultValue = "1") Integer pageNum,
                           @RequestParam(value = "pageSize",required = false,defaultValue = "5") Integer pageSize,
                           Model model){

        PageInfo<Orders> ordersPageInfo = ordersService.queryAllByPage(orders,pageNum,pageSize);
        model.addAttribute("ordersPageInfo",ordersPageInfo);
        return "orderslist";
    }
//    @GetMapping("/orders/{id}")
//    public String getOrdersdById(@PathVariable("id") int id,Model model){
//        Orderdetail orderdetail= getOrdersdById(id);
//        model.addAttribute("orderdetail",orderdetail);
//        return "ordersdlist";
//    }



    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
//    @GetMapping("{id}")
//    public ResponseEntity<Orders> queryById(@PathVariable("id") Integer id) {
//        return ResponseEntity.ok(this.ordersService.queryById(id));
//    }
    @GetMapping("/getOrdersById")
    public Orders selectOne(Integer id) {
        return this.ordersService.queryById(id);
    }

    /**
     * 新增数据
     *
     * @param orders 实体
     * @return 新增结果
     */
//    @PostMapping
//    public ResponseEntity<Orders> add(Orders orders) {
//        return ResponseEntity.ok(this.ordersService.insert(orders));
//    }
    @PostMapping("/addorder")
    public Orders addorder(Orders orders){
        return  this.ordersService.insert(orders);

    }
    /**
     * 编辑数据
     *
     * @param orders 实体
     * @return 编辑结果
     */
//    @PutMapping
//    public ResponseEntity<Orders> edit(Orders orders) {
//        return ResponseEntity.ok(this.ordersService.update(orders));
//    }
    @PostMapping("/editorders")
    public Orders editorders(Orders orders){
        return  this.ordersService.update(orders);
    }
    /**
     * 删除数据
     *
     * @param oid 主键
     * @return 删除是否成功
     */
//    @DeleteMapping
//    public ResponseEntity<Boolean> deleteById(Integer id) {
//        return ResponseEntity.ok(this.ordersService.deleteById(id));
//    }
    @GetMapping("/delOrderById/{oid}")
    public String deleteById(@PathVariable("oid") Integer oid){
        this.ordersService.deleteById(oid);
        return "forward:/admin/orders";
    }

}

